﻿using Abp.EntityFramework;
using MyCompanyName.AbpZeroTemplate.IRepositorys;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MyCompanyName.AbpZeroTemplate.EntityFramework.Repositories
{
    public class CardRepository : AbpZeroTemplateRepositoryBase<Card.Card>, ICardRepository
    {
        private IDbContextProvider<AbpZeroTemplateDbContext> _dbContextProvider;
        public CardRepository(IDbContextProvider<AbpZeroTemplateDbContext> dbContextProvider) :
            base(dbContextProvider)
        {
            this._dbContextProvider = dbContextProvider;
        }

        public List<Card.Card> UpdateAgentCodeForCard(string agentCode, string uccidStart, string uccidEnd)
        {
            try
            {
                string sql = string.Empty;
                if (!agentCode.Equals("admin"))
                {
                    sql = string.Format(@"SELECT
	                                        *
                                        FROM
	                                        Cards
                                        WHERE
	                                        SUBSTRING (UCCID, 13, 8) BETWEEN {0} AND {1}  and AgentCode='{2}'", uccidStart, uccidEnd, agentCode);
                }
                else
                {
                    sql = string.Format(@"SELECT
	                                        *
                                        FROM
	                                        PrepareCards
                                        WHERE
	                                        SUBSTRING (UCCID, 13, 8) BETWEEN {0} AND {1}", uccidStart, uccidEnd);
                }
                return _dbContextProvider.GetDbContext().Database.SqlQuery<Card.Card>(sql).ToList();
            }
            catch (Exception ex)
            {

                throw new Exception(ex.ToString());
            }
        }
    }
}
